package com.winner.data.util.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * easyExcel：export handle 导出excel冻结表头和冻结指定列并支持筛选器
 * freeze row and col with set auto filter range
 * @author wangxinle5
 * @date: 2024-1-5 13:44:26
 */
public class FreezeRowColHandler implements SheetWriteHandler {

    private final FreezeRowColOptions options;

    public FreezeRowColHandler(FreezeRowColOptions options) {
        this.options = options;
    }


//    colSplit:表示要冻结的列数；
//    rowSplit:表示要冻结的行数；
//    leftmostColumn:表示被固定列右边第一列的列号；
//    topRow:表示被固定行下边第一列的行号
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.createFreezePane(options.getColSplit(), options.getRowSplit(), options.getLeftmostColumn(), options.getTopRow());
        if (null != options.getAutoFilterRange()) {
            sheet.setAutoFilter(CellRangeAddress.valueOf(options.getAutoFilterRange()));
        }
    }


}
